<?php
// +----------------------------------------------------------------------
// | OneThink [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013 http://www.onethink.cn All rights reserved.
// +----------------------------------------------------------------------
// | Author: Ringo Chan <334258471@qq.com> <http://www.jxlnxx.com>
// +----------------------------------------------------------------------

namespace Home\Controller;

/**
 * 前台抢购会控制器
 * 主要获取抢购会聚合数据
 */
class QghController extends HomeController {
    
    /**
     * 抢购会首页
     * 
     * @author Ringo Chan <334258471@qq.com>
     */
    public function index(){
       
        $M_product=M('product');
        $M_product_image=M('product_image');
        $special_panic=M('special_panic');
        
        $page=I('page',1);
        $pagesize=I('pagesize',10);
        $page <=0 && $page=1;
        $pagesize <=0 && $pagesize=10;
        $offset=($page-1)*$pagesize;
      
        
        //*************
        
        $cid = I('post.cid'); // 商品分类编号
       
        $panic_id = I('post.panic_id'); // 抢购会时间段编号
        $panic_sortdiscount = I('post.panic_sortdiscount', 0); // TRUE：根据抢购会折扣排序
        $panic_endtime = I('post.panic_endtime'); // 已开始抢购但尚未结束的商品
        
        $options = array();
        $model = M('product');
        $type = 'putong';
        $extend=I('extend');
        $product_list=array();   
        
        $prefix = C('DB_PREFIX');
        $l_table = $prefix.('product');
        $m_table = $prefix.('product_special_panic');
        $r_table = $prefix.('special_panic');
    
        $model = $model->table($l_table.' l')->
        join($m_table.' m ON m.product_id=l.id')->join($r_table.' r ON r.id = m.special_panic_id');
    
        $map['l.status'] = 1;
        $map['l.is_show'] = 1;
        $map['l.in_special_panic'] = 1;
        $map['l.type'] = $type;
    

        $M_product_category=M('product_category');
        $cid=I('request.cid');
        $fid=I('request.fid');
        if($cid){
            $map['l.category_id']=$cid;
        }
        else if($fid){
            $cids=$M_product_category->where(array('pid'=>$fid))->field('id')->select();
            $cids=arr2tostr($cids);
            $map['l.category_id']=array('in',$cids);
        }
        
        //!(empty($category_id) || !is_numeric($category_id)) && $map['l.category_id'] = $category_id;
        $panic_id && $map['m.special_panic_id'] = $panic_id;
        $panic_endtime && $map['r.end_time'] = array('elt', $panic_endtime);
        
        $options['field'] = array('l.id','l.category_id', 'l.title', 'l.price', 'l.quantity','l.sales');
        $options['where'] = $map;
        
        if($extend){
            switch($extend){
                case  'most':
                    $options['order']='m.discount ASC';
                    break;
                case  'last':
                    $options['order']='l.end_time ASC';
                    break;
            }
        }
        
        $OPT = new \ReflectionProperty($model,'options');
        $OPT->setAccessible(true);
        
        $options = array_merge( (array)$OPT->getValue($model), $options );
        $total = $model->where($options['where'])->count();
        
        if ($page > 0 && $pagesize > 0) {
            $start = ($page - 1) * $pagesize;
            $options['limit'] = $start.','.$pagesize;
        }       
        $model->setProperty('options',$options);   
    
        $product_list = $model->select();
       
        //---------------        
        
        foreach ($product_list as &$item) {
            $item['price']=(int)$item['price'];
            $image_info = $M_product_image->where(array('product_id'=>$item['id']))->order('id ASC')->find();
             
            $item['image'] = get_cover($image_info['cover_id'],'path');
             
        }
        //热销
        $saletopmap=array();
        $saletopmap['type']='putong';
        $saletopmap['in_special_panic']=1;
        $saletopmap_list=$M_product->where($saletopmap)->limit(0,6)->order('sales DESC')->select();
        
        foreach ($saletopmap_list as &$item) {
            $item['price']=(int)$item['price'];
            $image_info = $M_product_image->where(array('product_id'=>$item['id']))
            ->order('id ASC')->find();
            $item['image'] = get_cover($image_info['cover_id'],'path');
        }
        $this->assign('salestop',$saletopmap_list);
        
        //广告
        $ad_positon=M('ad_position')->where(array('identifier'=>'ad_qiangotop'))->find();
        
        $adlist=M('ad')->where(array('position_id'=>$ad_positon['id']))
        ->order('sort DESC')->field('cover_id,title,link,product_id')->select();
        foreach($adlist as &$item){            
            $item['image']=get_cover($item['cover_id'],'path');
        }
        
        
         /*抢购场次 开始*/
        $deadline_start = date('Y-m-d 00:00:00', NOW_TIME);
        $deadline_end = date('Y-m-d 23:59:59', NOW_TIME);
         
        $panicmap=array();
      
        $panicmap['start_time']=array('egt', strtotime($deadline_start));
        $panicmap['end_time']=array('elt',strtotime($deadline_end));
        
        $spnic_schedule =$special_panic->where($panicmap)->select();
       
        $now_hours=(int)date('H' ,NOW_TIME);
        
        $hour_sh=array();
        $time_status=getStatus('hours');        
    
        foreach ($spnic_schedule as &$item){
            if(count($hour_sh) ==24){
                break;
            }            
            $start_time_hour=(int)date('H' , $item['start_time']);            
            $end_time_hour=(int)date('H' , $item['end_time']);
            while($start_time_hour <=$end_time_hour){
                $hour_sh[$start_time_hour]['hour']=$time_status[$start_time_hour];
                $hour_sh[$start_time_hour]['text']=  $start_time_hour <= $now_hours ? '已开抢' : '未开抢';
               $now_hours ==  $start_time_hour && $hour_sh[$start_time_hour]['curr']=1;
                $start_time_hour ++;
            }
        }
       
        $sh_total=count($hour_sh);
        $hour_sh_assign=array();
        $offset=$now_hours-2;
        $loopstop=0;        
       
        $sh_total <=8 && $offset=0;
        
        foreach($hour_sh as $key => $value)
        {
            if(count($hour_sh_assign) ==8) {
                break;
            }            
            if($key < $offset){                         
                continue;
            }
            if(isset($value)){               
                    $hour_sh_assign[$key] =$value;
                    $offset++;
            }
        }
     
        $this->assign('spnic_schedule',$hour_sh_assign);
        /*抢购场次 结束 */
      
        $this->assign('end_time',strtotime($deadline_end));
       
        
        $this->assign('adlist',$adlist);
        
        $this->assign('p_list',$product_list);
        
        $this->display();
    }
}
?>